Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Method

Parameter & Arguments

In Java, methods can take parameters, which are values that you can pass into methods to customize their behavior. Here’s a detailed explanation:

Parameters and Arguments

Parameters are variables defined in the method declaration after the method name, inside the parentheses. They act as variables inside the method. Arguments are the actual values that are passed into the method when the method is called. They replace the parameters in the method definition when the method is executed. Here’s an example to illustrate this:
Method parameter and arguments example in java public class Main { static void myMethod(String fname) { System.out.println(fname + " method"); } public static void main(String[] args) { myMethod("Liam"); myMethod("Jenny"); myMethod("Anja"); } }

Output

Liam method Jenny method Anja method
In this example, fname is a parameter, while “Liam”, “Jenny”, and “Anja” are arguments.

Types of Parameters

Primitive Parameters: These include types such as int, float, boolean, etc. Non-Primitive or Object Parameters: These include types such as String, Array, etc. Here’s an example of a method with multiple parameters:
Method parameter and arguments example public class Main { static void myMethod(String fname, int age) { System.out.println(fname + " is " + age); } public static void main(String[] args) { myMethod("Liam", 5); myMethod("Jenny", 8); myMethod("Anja", 31); } }

Output

Liam is 5 Jenny is 8 Anja is 31
In this example, fname and age are parameters, while “Liam”, 5, “Jenny”, 8, “Anja”, and 31 are arguments. Note: When working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

  📌TAGS

★Class ★ Method ★ Object ★ java ★ oops ★ parameter ★ argument

Tutorials